package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.RequestAndResultCode;
import com.bjesc_b2c.app.appraiset.adapter.CarfromTwoAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.mvp.presenter.GetCarForPresenter;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.util.MapUtil;

import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CarForBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/5/23.
 */

public class MarketActivity extends BaseActivity{
    @InjectView(R.id.recycler_view)
    RecyclerView carfromTwoRecycler;
    @InjectView(R.id.tv_title)
    TextView titleTv;
    private CarfromTwoAdapter twoAdapter;
    private List<CarForBean.TwoBean> twoBeanList;
    private List<CarForBean> carForBean;
    private String marketName;
    private Intent intent;
    private String marketId;
    private LinearLayoutManager twoManager;
    private GetCarForPresenter carForPresenter;

    private int type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_from);
        ButterKnife.inject(this);
        intent = new Intent();
        titleTv.setText("选择车市");
        type = getIntent().getIntExtra("TYPE",1);
        requestCarMarket(this);
    }

    private void initViews() {
        twoAdapter = new CarfromTwoAdapter(this, R.layout.layout_item_carfrom_two, carForBean,CarfromTwoAdapter.TYPE_TWO);
        twoManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        carfromTwoRecycler.setLayoutManager(twoManager);
        carfromTwoRecycler.setAdapter(twoAdapter);
        twoAdapter.setOnItemClickListener(twoListener);
    }


    private MultiItemTypeAdapter.OnItemClickListener twoListener = new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            marketName = carForBean.get(position).getMarket_name();
            marketId = carForBean.get(position).getId();
            intent.putExtra("marketName", marketName);
            intent.putExtra("marketId", marketId);
           switch (type){
               case 1 ://只显示车市
                   setResult(RequestAndResultCode.SelectColorActivity_ResultCode, intent);
                   finish();
                   break;
               case 2://跳转到车行选择界面
                   intent.setClass(MarketActivity.this,CarFromActivity.class);
                   startIntent(intent,2);
                   break;
           }
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };

    @OnClick(R.id.iv_return)
    public void onClick() {
        finish();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 3) {
            Intent intent = new Intent();
            intent.putExtra("carForName", data.getStringExtra("carForName"));
            intent.putExtra("carFofId", data.getStringExtra("carFofId"));
            setResult(RequestAndResultCode.CarFromActivity_ResultCode, intent);
            Logger.d("onActivityResult");
            finish();
        }
    }

    public void requestCarMarket(Context context) {
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, new MapUtil().build()).create(IApiActivityRequest.class).getMarket(),
                new BaseSubscriber<TokenBean<List<CarForBean>>>(context) {
                    @Override
                    public void onSuccess(TokenBean<List<CarForBean>> o) {
                        carForBean = o.getData();
                        initViews();
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }
}
